package gw.com.android.ui.onlive;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

/**
 * 晒单模板的父布局。。。。
 *
 * @author jett
 * @since 2018-05-30.
 */
public class DiscussTempletFrameLayout extends FrameLayout {

    public DiscussTempletFrameLayout(@NonNull Context context) {
        super(context);
    }

    public DiscussTempletFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DiscussTempletFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        scaleChild();
    }

    private void scaleChild() {
        View child = getChildAt(0);
        if (child != null) {
            int width = getMeasuredWidth();
            int height = getMeasuredHeight();
            if (width > 0 && height > 0) {
                ViewGroup.LayoutParams params = child.getLayoutParams();
                int childWidth = params.width;
                int childHeight = params.height;
                child.setScaleX(width * 1.0F / childWidth);
                child.setScaleY(height * 1.0F / childHeight);
            }
        }
    }

}
